package edu.hunau.cxb19.oop.class26;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;

import org.junit.Test;

/**
 *
 * @author hb
 *
 *         2020-03-31 15:56:43
 */
public class CharStreamTest {

	@Test
	public void writerTest() throws Exception {
		try ( // 获得字节流
				FileOutputStream fos = new FileOutputStream(".\\demo\\out02.txt", true);
				// 获得字符流,同时指定字符编码
				OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");) {
			osw.write("你是一个中央空调!");
			osw.write("\n");
			osw.flush();
		}
	}

	@Test
	public void readerTest() throws Exception {
		try (   FileInputStream fis = new FileInputStream(".\\demo\\out02.txt");
				InputStreamReader isr = new InputStreamReader(fis, "utf-8");) {
			
			int datas=-1;
			while((datas=isr.read())!=-1) {
				System.out.print((char)datas);
			}

		}

	}
	
	@Test
	public void fileReaderTest()  throws Exception{
		try(FileReader fr = new FileReader(".\\demo\\out02.txt");){
			int datas=-1;
			while((datas=fr.read())!=-1) {
				System.out.print((char)datas);
			}
		}
	}
	
	public void buffWriterTest() throws Exception {
		try(FileWriter fw = new FileWriter(".\\demo\\out03.txt", Charset.forName("utf-8"), true);
		BufferedWriter bw = new BufferedWriter(fw);){
			bw.write("要下课了");
			bw.newLine();
			bw.write("好多类，搞不清");
			bw.flush();
		}
	}
	
	
	@Test
	public void  buffReaderTest()  throws Exception{
		try(FileReader fr = new FileReader(".\\demo\\out02.txt");
			BufferedReader br=	new BufferedReader(fr);){
			String datas=null;
			while((datas=br.readLine())!=null) {
				System.out.println(datas);
			}
		}
	}
	

}
